<?php
/**
 * created by zzy
 * date: 2018/1/26 14:04
 */
namespace App\Eloquent\Ygt;

class WarehouseOut extends DbEloquent{

    protected $table    = 'ygt_warehouse_out';

    public static function getSn()
    {
        $nowTime            = $_SERVER['REQUEST_TIME'];
        $y4md               = date('Ymd',$nowTime);
        $nowTime0           = strtotime($y4md);
        $y2md               = date('ymd',$nowTime);
        $where              = [
            ['created_at','>',$nowTime0]
        ];
        $count              = self::where($where)->count();
        $count ++;
        if($count<10)
        {
            $count          = '0'.$count;
        }
        $sn                 = 'CKD'.$y2md.$count;
        return $sn;
    }

    public static function getListAndRelation($where,$limit='',$offset='',$groupby='')
    {
        $columns            = 'ygt_warehouse_out.sn,ygt_warehouse_out.status';
        $columns            .= ',ygt_warehouse_out_relation.*';
        $orderby            = ['ygt_warehouse_out.id','desc'];
        $fieldL             = 'ygt_warehouse_out.id';
        $fieldR             = 'ygt_warehouse_out_relation.warehouse_out_id';
        $join               = [
            ['table'=>'ygt_warehouse_out_relation', 'field_l'=>$fieldL, 'field_c'=>'=', 'field_r'=>$fieldR],
        ];
        $collection         = self::getList($where,$columns,$limit,$offset,$orderby,$groupby,$join);
        return $collection;
    }

    public static function changeRealtionData($relationList,$orderList)
    {
        $result             = [];
        foreach($relationList as $key=>$val)
        {
            foreach($orderList as $k=>$v)
            {
                if($val['order_id'] == $v['order_id'])
                {
                    $arr                = $val;
                    $arr['product_name']    = $v['product_name'];
                    $arr['img_path']    = $v['img_path'];
                    $arr['customer_name']    = $v['customer_name'];
                    $arr['order_type_title']    = $v['order_type_title'];
                    $arr['field_name_23']    = $v['field_name_23'];
                    $arr['finished_specification']    = $v['finished_specification'];
                    $arr['default_num']    = 0;
                    $arr['default_time']    = '';
                    //获取工单表的成品重量信息
                    $finishedWeight = '';
                    $tmpOrderRow = \App\Eloquent\Ygt\Order::where(['id'=>$v['order_id']])->first();
                    if($tmpOrderRow && $tmpOrderRow['finished_weight']){
                        $finishedWeight = $tmpOrderRow['finished_weight'];
                    }

                    $arr['weight']    = $finishedWeight;
                    $arr['address_address']    = $val['province_name'].$val['city_name'].$val['area_name'].$val['address'];
                    $result[]       = $arr;
                }
            }
        }
        return $result;
    }
    public static function getRealtionData($outList,$relationList,$orderList)
    {

        $result             = [];
        $relationListArr    = self::changeRealtionData($relationList,$orderList);
        $relationListArr    = collect($relationListArr)->groupby('warehouse_out_id')->toArray();
        foreach($outList as $key=>$val)
        {
            $valId          = $val['id'];
            $valUpdate      = $val['updated_at'];
            $valUpdateYmd   = '';
            if($valUpdate)
            {
                $valUpdateYmd   = date('Y-m-d',$valUpdate);
            }
            if(isset($relationListArr[$valId]))
            {
                $tempData       = $relationListArr[$valId];
                foreach($tempData as $k=>$v)
                {
                    $tempData[$k]['deal_date']  = $valUpdateYmd;
                    $tempData[$k]['weight'] = \App\Engine\Common::fieldDataDeal($tempData[$k]['weight'],1);//处理数量+单位的数据
                }
                $size           = count($tempData);
                //如果是已发货的出库单，获取发货单号
                $tempArr        = [
                    'sn'=>$val['sn'],
                    'size'=>$size,
                    'data'=>$tempData,
                    'deal_date'=>$valUpdateYmd,
                ];

                if($val['deal_status'] == 1){
                    $warehouseSendRelationRow = \App\Eloquent\Ygt\WarehouseSendRelation::where(['warehouse_out_id'=>$val['id']])->first();
                    if($warehouseSendRelationRow){
                        $tempArr['warehouse_send_id'] = $warehouseSendRelationRow->warehouse_send_id;
                    }else{
                        $tempArr['warehouse_send_id'] = 0;
                    }
                }

                $result[]       = $tempArr;
            }
        }
        return $result;
    }

}
